package com.zm.demo.spring.tx.service;

import com.zm.demo.spring.tx.mapper.UserMapper;
import com.zm.demo.spring.tx.po.User;
import org.apache.skywalking.apm.toolkit.trace.TraceCrossThread;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional(propagation = Propagation.NOT_SUPPORTED, rollbackFor = {Exception.class})
@Service("userService")
public class UserServiceImpl implements UserService{

    @Autowired
    UserMapper userMapper;
    Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);

    @Override
    public int insertUser(User entity) {
        return userMapper.insert(entity);
    }

    @Override
    public List<User> getUsers() {
        long start = System.currentTimeMillis();
        List<User> users = userMapper.selectList(null);
        long end = System.currentTimeMillis();
        logger.info("{}, end : {},  cost {} ms",Thread.currentThread().getName() , end ,end - start);
        return users;
    }


    @Override
    public int updateById(User entity) {
        return userMapper.updateById(entity);
    }

    @Override
    public int updateBatch(List<User> users) {
        throw new RuntimeException("更新异常");
//        return userMapper.updateBatch(users);
    }

    @Override
    public int updateBatch1(List<User> users) {
        return userMapper.updateBatch1(users);
    }


}
